/**
 * Copyright 2007-2009 Vidar Svansson
 *
 * This file is part of JAdapter.
 *
 * JAdapter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JAdapter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JAdapter.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jadapter.spring.config;

import org.jadapter.registry.TransientAdapterRegistry;
import org.jadapter.registry.TransitiveAdapterRegistry;
import org.jadapter.spring.SpringAdapterRegistry;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 *
 * @author Vidar Svansson
 * @since 0.3
 */
public class AdapterRegistryDefinitionParser extends AbstractSingleBeanDefinitionParser {


    protected Class getBeanClass(Element element) {
        return SpringAdapterRegistry.class;
    }


    protected void doParse(Element element, ParserContext parserContext,
                           BeanDefinitionBuilder bean) {

        String mode = element.getAttribute("mode");

        Class transformerRegistry;

        if(mode == null || "transitive".equals(mode)){
            transformerRegistry = TransitiveAdapterRegistry.class;
        } else {
            transformerRegistry = TransientAdapterRegistry.class;
        }


        BeanDefinitionBuilder factoryBeanBuilder =
                BeanDefinitionBuilder.genericBeanDefinition(transformerRegistry);

        BeanDefinition factoryBean = factoryBeanBuilder.getBeanDefinition();

        parserContext.getRegistry().registerBeanDefinition("transformerRegistry", factoryBean);


    }

}
